import type { ActivityItem } from '@/service/app'
import dayjs from 'dayjs'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { calendarActivityControllerFindAll, calendarActivityConfigurationControllerFindAll as getConfigTypeAPI } from '@/service/app'

interface ActivityTypes {
  id: number
  title: string
  color: string
}

export interface CalendarItem {
  date: string
  values: ActivityItem[]
}

export const useCalendarStore = defineStore(
  'calendar',
  () => {
    const types = ref<ActivityTypes[]>([])
    const calendarData = ref<CalendarItem[]>([])

    const getConfigType = async () => {
      const { data, success }: any = await getConfigTypeAPI({})

      if (success) {
        types.value = data
      }
    }

    /** 获取日期内数据，'YYYY-MM-DD'格式 */
    const getCalendarData = async () => {
      const currentDate = dayjs()
      const startDate = currentDate.clone().startOf('M').format('YYYY-MM-DD')
      const endDate = currentDate.clone().endOf('M').format('YYYY-MM-DD')

      const { data, success }: any = await calendarActivityControllerFindAll({
        params: {
          startDate,
          endDate,
          time: Date.now()
        },
      })

      if (success) {
        console.log('🚀日历数据请求成功：', data)
        calendarData.value = data
      }
      else {
        console.log('🐛日历数据请求失败！')
      }
    }

    return {
      getConfigType,
      types,
      getCalendarData,
      calendarData,
    }
  },
  {
    persist: true,
  },
)
